package com.fernfx.xingtan.main.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.main.contract.RobredPacketContract;
import com.fernfx.xingtan.main.model.RobredPacketModel;
import com.fernfx.xingtan.my.contract.MySettingContract;
import com.fernfx.xingtan.my.entity.QiniuEntity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.fernfx.xingtan.view.dialog.ActionSheetDialog;

import java.util.Map;

/**
 * Created by admin on 2018/8/22.
 */

public class RobredPacketPresenter<P extends RobredPacketContract.View> implements RobredPacketContract.Presenter {
    private P P;
    //model
    private RobredPacketContract.Mode model;

    @Override
    public void init(BaseView view) {
        this.P = (P)view;
        this.model = new RobredPacketModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {

    }

    @Override
    public void showUserLogoDialog(Context context) {
        new ActionSheetDialog(context)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .addSheetItem("打开相机", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                P.autoObtainCameraPermission();
                            }
                        }).addSheetItem("打开相册", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        P.autoObtainStoragePermission();
                    }
                }).show();
    }

    @Override
    public void getQiniuToken() {
        model.getQiniuToken(new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                P.getActivity().hideLoading();
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);onNetworkError(result.getMessage(),500);
                    return;
                }
                QiniuEntity qiniuEntity = FastJsonUtil.fromBean(message, QiniuEntity.class);
                if (null != qiniuEntity && OtherUtil.checkRequestStatus(qiniuEntity.getSuccess(),qiniuEntity.getStatus())) {
                    P.setQiniuToken(qiniuEntity.getMsg());
                }else {
                    P.getActivity().hideLoading();
                    ToastUtil.showCentertoast(qiniuEntity.getMsg());
                }
            }
        });
    }

    @Override
    public void detachView() {

    }
}
